package com.touchealth.test.route;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.codec.binary.Base64;

import java.net.URI;
import java.net.URL;
import java.security.MessageDigest;
import java.util.*;

/**
 * @author zhangyanqiang
 * @date 2020/4/13
 **/
public class CaiNiaoRoute {

    public static void main(String[] args) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appName","ZJJP");
        jsonObject.put("cpCode","STO");
        jsonObject.put("mailNo","770001113650200");
        String charset="utf-8";
        String logistic_provider_id ="e8137f4b9dcb7ee57c1bcf0cd30f1014";
        String msg_type="trace_info_query";
        String keys="3D7WX52CVelQm7Re5AbGO18Pn7M83r3K";
        String to_code="LD-PACKPUSH";
        String md5=doSign(jsonObject.toString(),charset,keys);
        Map<String,Object> map= new HashMap<>();
        map.put("logistics_interface",jsonObject.toString());
        map.put("logistic_provider_id",logistic_provider_id);
        map.put("msg_type",msg_type);
        map.put("data_digest",md5);
        map.put("to_code",to_code);
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        String url="http://link.cainiao.com/gateway/link.do";
        try{
            httpClient =  HttpClients.createDefault();

            URL url1 = new URL(url);
            URI uri = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null);
            httpPost = new HttpPost(uri);
            //设置参数
            List<org.apache.http.NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();

            while(iterator.hasNext()){
                Map.Entry<String,String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
                System.out.println( result);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static String doSign(String content, String charset, String keys) {
        String sign = "";
        content = content + keys;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(content.getBytes(charset));
            sign = new String(Base64.encodeBase64(md.digest()), charset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return sign;
    }
}
